package com.powerunion.datacollection.web.struts;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.extjs.BasePara;
import com.powerunion.extjs.DataSource;
import com.powerunion.extjs.ListRange;
import com.powerunion.extjs.TreeNode;
import com.powerunion.util.log.LogHelper;

public abstract class BaseAction extends DispatchAction {
	private static final Logger logger = LogHelper.getInstance().getLogger(BaseAction.class);
	private DataSource dataSource = null;
    public BaseAction() {
        super();
        this.dataSource = new DataSource();
    }

    public void finalize() throws Throwable {
       super.finalize();
    }

    /**
     * 返回权限不足页面
     * @param mapping ActionMapping
     * @return ActionForward
     */
    public ActionForward denyForward(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        return mapping.findForward("right.deny");
    }
    
	public static final String SUCCESS = "{success:true}";
	public static final String FAILURE = "{failure:true}";

	public static final String DIRECTLY_MESSAGE_KEY = "message";
	
	/**
	 * 直接输出.
	 * 
	 * @param contentType 内容的类型.html,text,xml的值见后，json为"text/x-json;charset=UTF-8"
	 */
	protected void render(HttpServletResponse response, String text,String contentType){
		try {
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}
	/**
	 * 直接输出纯json格式字符串
	 */
	protected void renderJson(HttpServletResponse response, String text){
			this.render(response, text, "text/json;charset=utf-8");
	}
	
	/**
	 * 直接输出纯字符串.
	 */
	protected void renderText(HttpServletResponse response, String text) {
		render(response,text,"text/plain;charset=UTF-8");
	}

	/**
	 * 直接输出纯HTML.
	 */
	protected void renderHtml(HttpServletResponse response, String text) {
		render(response,text,"text/html;charset=UTF-8");
	}

	/**
	 * 直接输出纯XML.
	 */
	protected void renderXML(HttpServletResponse response, String text) {
		render(response,text,"text/xml;charset=UTF-8");
	}
	/**
	 * 直接输出代表成功的json字符串
	 */
	protected void renderSuccssJson(HttpServletResponse response){
		this.renderJson(response, SUCCESS);
	}
	/**
	 * 直接输出代表失败的json字符串
	 */
	protected void renderFAILUREJson(HttpServletResponse response){
		this.renderJson(response, FAILURE);
	}
	
	/**
	 * 直接输出代表成功的json字符串
	 */
	protected void renderSuccssJson(HttpServletResponse response,String data){
		this.renderJson(response, "{success:true,data:'"+data+"'}");
	}
	/**
	 * 直接输出代表失败的json字符串
	 */
	protected void renderFAILUREJson(HttpServletResponse response,String data){
		this.renderJson(response, "{failure:true,data:'"+data+"'}");
	}
	
	public ActionForward getTreeData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sqlpath =  request.getParameter("sqlpath");	
		String queryStr =  request.getParameter("queryStr");
		String queryValue =  request.getParameter("queryValue");
		BasePara basePara = new BasePara();
		basePara.setSqlpath(sqlpath);
		basePara.setQueryStr(queryStr);
		basePara.setQueryValue(queryValue);
		
		String paraId = request.getParameter("node");
		
		TreeNode treenode = new TreeNode();
		treenode.setId(paraId);
		
		List treeNodes = dataSource.getTreeData(treenode, basePara);
		JSONArray jsonarr = JSONArray.fromObject(treeNodes);

		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(jsonarr);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}//getComboData
	
	public ActionForward getComboData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sqlpath =  request.getParameter("sqlpath");
		BasePara basePara = new BasePara();
		basePara.setSqlpath(sqlpath);
		List datas = dataSource.getComboData(basePara);
		JSONArray jsonarr = JSONArray.fromObject(datas);
		StringBuffer result = new StringBuffer();
		result.append("{results:").append(jsonarr).append("}");

		try {
			this.renderJson(response, result.toString());
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getGridData(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String sqlpath =  request.getParameter("sqlpath");	
		String queryStr =  request.getParameter("queryStr");
		String queryValue =  request.getParameter("queryValue");
		BasePara basePara = new BasePara();
		basePara.setSqlpath(sqlpath);
		basePara.setQueryStr(queryStr);
		basePara.setQueryValue(queryValue);
		
		ListRange range = this.dataSource.getGridData(basePara);
		
		JSONObject results =  JSONObject.fromObject(range);
		
		logger.debug("grid datas:" + results.toString());
		try {
			this.renderJson(response, results.toString());
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}

	
}